import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Task } from '@/types/task'

export const taskUseStore = defineStore(
  'myDay',
  () => {
    // 任务列表

    const task_list = ref<Task[]>([])
    function addTaskList(item: string): void {
      task_list.value.push({
        id: Date.now(),
        content: item,
        isImportant: false,
        isFinished: false,
      })
      console.log(task_list.value)
    }

    // 改变完成还是未完成
    function changeIsFinished(item: Task): void {
      item.isFinished = !item.isFinished
    }
    function deleteTaskList(item: Task): void {
      task_list.value.splice(task_list.value.findIndex((res) => res.id === item.id),1)
      console.log(task_list.value)
    }
     function deleteAllTask(): void {
      task_list.value = []
     }
    // 记得 return
    return {
      task_list,
      addTaskList,
      changeIsFinished,
      deleteTaskList,
      deleteAllTask,
    }
  },
  {
    // 网页端配置
    persist: true,
    // 小程序端配置
    // persist: {
    //   storage: {
    //     getItem(key) {
    //       return uni.getStorageSync(key)
    //     },
    //     setItem(key, value) {
    //       uni.setStorageSync(key, value)
    //     },
    //   },
    // },
  }
)
